package org.render;

import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;

import org.geom.Line2D;
import org.geom.Point2D;
import org.geom.Point3D;
import org.geom.curve.ICurve;

public class CurveRenderer2D implements ICurveRenderer {

	/*
	 * -- DESCRIZIONE DA RIVEDERE --
	 * Class that render the projection of some complex shape in 3D space
	 * in 2D space, using as a projection plane one of the three view of
	 * standard ortogonal projection (as a 3d drawing) 
	 * 
	 * */
		
		//---------------------------------------------------------------------- ENUM
		/* TODO : change in ENUM */
		public static final String X = "x";
		public static final String Y = "y";
		public static final String Z = "z";
		
		//---------------------------------------------------------------------- class var
		
		//protected Point3D currentPoint = new Point3D();
		
		// projections var
		public String plane;
		
		protected int tx = 100;
		protected int ty = 100;
		
		//---------------------------------------------------------------------- constructor
		public CurveRenderer2D(String plane) {
			this.plane = plane;	
		}
		
		//---------------------------------------------------------------------- render
		//@Override
		public void render(ICurve curve, Graphics g) {
			//System.out.println("Rendered2D > tx: "+tx+" ty: "+ty+" t0x: "+t0x+" t0y: "+t0y);
			//System.out.println("Rendered2D > render points: "+points+" and graphics:"+g+" tx:"+tx);
			//System.out.println("CurveRenderer2D > render >  points: "+points);
			drawCenter(g);
			drawCurvePoints(curve.getPoints(), g);
			drawCurve(curve.getPoints(), g);
			
			drawCurrentPoint(curve.getCurrentPoint(), g);
		}
		
//		@Override
//		public void render(ArrayList<Point3D> points, IGraphicsWrapper g) {
//			// TODO Auto-generated method stub
//			
//		}

		//---------------------------------------------------------------------- 2d core render
		protected void drawCurrentPoint(Point3D currentPoint, Graphics g) {
			int k = 10;
			Point2D p = this.transformCoordinateOnProjectionPlane(currentPoint);
			g.setColor(new Color(0,132,209));
			g.fillOval((int)p.x +tx -k/2, (int)p.y +ty -k/2, k, k);
		}

		protected void drawCurvePoints(ArrayList<Point3D> points, Graphics g) {
			//System.out.println("CurveRenderer2D > drawCurvePoints");
			Point2D p2 = new Point2D();
			
			int k = 6;
			
			for( Point3D p3 : points) {
				p2 = transformCoordinateOnProjectionPlane(p3);
				
				//System.out.println("CurveREnderer2D > drawCurvePoints > for > p2: "+p2);
				//System.out.println("CurveREnderer2D > drawCurvePoints > for > p3: "+p3);
				
				g.setColor(new Color(240,20,154));
				g.drawOval((int)p2.x +tx -k/2, (int)p2.y +ty -k/2, k, k);
				g.setColor(new Color(240,20,154, 120));
				g.fillOval((int)p2.x +tx -k/2, (int)p2.y +ty -k/2, k, k);
			}
		}

		protected void drawCurve(ArrayList<Point3D> points, Graphics g) {
			Point2D p2 = new Point2D();
			Point2D p2pre = transformCoordinateOnProjectionPlane(points.get(0));//new Point2D();

			g.setColor(Color.BLACK);
			for( Point3D p3 : points) {
				p2 = transformCoordinateOnProjectionPlane(p3);
				g.drawLine( (int)p2pre.x +tx, 
						    (int)p2pre.y +ty, 
						    (int)p2.x +tx, 
						    (int)p2.y +ty );
				p2pre = p2;
			}
		}
		
		protected void drawLine(Line2D l, Graphics g) {
			g.drawLine((int)l.point1.x +tx, 
					(int)l.point1.y +ty, 
					(int)l.point2.x +tx,
					(int)l.point2.y +ty);
		}

		protected void drawCenter(Graphics g) {
			//System.out.println("CurveRenderer2D > drawCenter > g: "+g);
			g.setColor(new Color(0, 132, 209));
			g.drawLine(tx, ty-5, tx, ty+5);
			g.drawLine(tx-5, ty, tx+5, ty);
	        //g.fillRect(tx,ty,5,5);
		}

		//---------------------------------------------------------------------- 3d calcolous
		//---------------------------------------------------------------------- 
		//---------------------------------------------------------------------- 
		
		@Override
		public Point2D transform3dTo2d(Point3D p) {
			return transformCoordinateOnProjectionPlane(p);
		}
		
		protected Point2D transformCoordinateOnProjectionPlane(Point3D p3) {
			// figure 1
			Point2D p2 = new Point2D();
			if (plane==X) {
				p2.x = p3.x;
				p2.y = p3.y;
			} if (plane==Y) {
				p2.x = p3.z;
				p2.y = p3.y;
			} if (plane==Z) {
				p2.x = p3.x;
				p2.y = p3.z;
			}
			return p2;
		}
		
		//---------------------------------------------------------------------- getter && setter
		public void setTranslate(int x, int y) {
			tx += x;//t0x+x;
			ty += y;//t0y+y;
		}
		
		public Point2D getTranslate() {
			return new Point2D(tx, ty);
		}
		
		public void setTranslateCenter(int x, int y) {}
		public void setRotate(int x, int y) {}
		public void setRotateCenter(int y) {}
		public void setScale(int x, int y) {}
		public void setScaleCenter(int x, int y) { }
		public void setCurrentPoint(float t) { }



}
